﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Microsoft.Practices.Unity;
using BugTracker.DataAccess.Data;
using System.Web.Configuration;
using BugTracker.BusinessLogic.Attachments;
using BugTracker.BusinessLogic.BTUser;
using BugTracker.BusinessLogic.Company;
using BugTracker.BusinessLogic.Project;
using BugTracker.BusinessLogic.WorkLog;
using BugTracker.BusinessLogic.Issue;
using BugTracker.BusinessLogic.ReportedBy;
using BugTracker.BusinessLogic.Project.ProjectDetail;
using BugTracker.BusinessLogic.BTUser.ReportedBy;
using BugTracker.BusinessLogic.Issue.Details;

namespace BugTracker.WebUX.WebCode
{
    public class IoCBootstrap
    {
        private IUnityContainer container = null;

        public IUnityContainer Container
        {
            get { return container; }
            set { container = value; }
        }

        public IoCBootstrap()
        {
            try
            {
                Configure();
            }
            catch (ApplicationException)
            {
                throw new ApplicationException("IoCBootstrap");
            }
        }

        private void Configure()
        {
            string localCSN = WebConfigurationManager.AppSettings["LocalConnection"];
            string localCS = WebConfigurationManager.ConnectionStrings[localCSN].ConnectionString;
            BugTrackerEntities db = new BugTrackerEntities();
            db.Connection.ConnectionString = localCS;
            container = new UnityContainer();

            container.RegisterType<IAttachmentMapper, AttachmentMapper>();
            container.RegisterType<IUserTypeMapper, UserTypeMapper>();
            container.RegisterType<IBTUserMapper, BTUserMapper>();
            container.RegisterType<ICompanyTypeMapper, CompanyTypeMapper>();
            container.RegisterType<ICompanyMapper, CompanyMapper>();
            container.RegisterType<IPlatformTypeMapper, PlatformTypeMapper>();
            container.RegisterType<IIssuePriorityTypeMapper, IssuePriorityTypeMapper>();
            container.RegisterType<IIssueResolutionTypeMapper, IssueResolutionTypeMapper>();
            container.RegisterType<IIssueSeverityTypeMapper, IssueSeverityTypeMapper>();
            container.RegisterType<IIssueStatusTypeMapper, IssueStatusTypeMapper>();
            container.RegisterType<IIssueTypeMapper, IssueTypeMapper>();
            container.RegisterType<IIssueMapper, IssueMapper>();
            container.RegisterType<IProjectMapper, ProjectMapper>();
            container.RegisterType<IBuildMapper, BuildMapper>();
            container.RegisterType<IVersionMapper, VersionMapper>();
            container.RegisterType<ICategoryMapper, CategoryMapper>();
            container.RegisterType<IReproducedMapper, ReproducedMapper>();
            container.RegisterType<IReportedByMapper, ReportedByMapper>();
            container.RegisterType<IWorkLogMapper, WorkLogMapper>();

            container.RegisterType<IAttachmentService, AttachmentService>(new InjectionConstructor(new object[] { db, container.Resolve<IAttachmentMapper>() }));
            container.RegisterType<IUserTypeService, UserTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IUserTypeMapper>() }));
            container.RegisterType<IBTUserService, BTUserService>(new InjectionConstructor(new object[] { db, container.Resolve<IBTUserMapper>() }));
            container.RegisterType<ICompanyTypeService, CompanyTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<ICompanyTypeMapper>() }));
            container.RegisterType<ICompanyService, CompanyService>(new InjectionConstructor(new object[] { db, container.Resolve<ICompanyMapper>() }));
            container.RegisterType<IPlatformTypeService, PlatformTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IPlatformTypeMapper>() }));
            container.RegisterType<IIssuePriorityTypeService, IssuePriorityTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssuePriorityTypeMapper>() }));       
            container.RegisterType<IIssueResolutionTypeService, IssueResolutionTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssueResolutionTypeMapper>() }));
            container.RegisterType<IIssueSeverityTypeService, IssueSeverityTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssueSeverityTypeMapper>() }));
            container.RegisterType<IIssueStatusTypeService, IssueStatusTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssueStatusTypeMapper>() }));
            container.RegisterType<IIssueTypeService, IssueTypeService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssueTypeMapper>() }));
            container.RegisterType<IIssueService, IssueService>(new InjectionConstructor(new object[] { db, container.Resolve<IIssueMapper>() }));
            container.RegisterType<IProjectService, ProjectService>(new InjectionConstructor(new object[] { db, container.Resolve<IProjectMapper>() }));
            container.RegisterType<IBuildService, BuildService>(new InjectionConstructor(new object[] { db, container.Resolve<IBuildMapper>() }));
            container.RegisterType<IVersionService, VersionService>(new InjectionConstructor(new object[] { db, container.Resolve<IVersionMapper>() }));
            container.RegisterType<ICategoryService, CategoryService>(new InjectionConstructor(new object[] { db, container.Resolve<ICategoryMapper>() }));
            container.RegisterType<IReproducedService, ReproducedService>(new InjectionConstructor(new object[] { db, container.Resolve<IReproducedMapper>() }));
            container.RegisterType<IReportedByService, ReportedByService>(new InjectionConstructor(new object[] { db, container.Resolve<IReportedByMapper>() }));
            container.RegisterType<IWorkLogService, WorkLogService>(new InjectionConstructor(new object[] { db, container.Resolve<IWorkLogMapper>() }));
            container.RegisterType<IProjectDetailService, ProjectDetailService>(new InjectionConstructor(new object[] { db }));
            container.RegisterType<IReportedByDetailService, ReportedByDetailService>(new InjectionConstructor(new object[] { db }));
            container.RegisterType<IWorkLogDetailService, WorkLogDetailService>(new InjectionConstructor(new object[] { db }));
            container.RegisterType<IIssueDetailService, IssueDetailService>(new InjectionConstructor(new object[] { db }));
        }
    }
}